package com.xhwl.centiotdata.handler.account.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.account.AccountFactory;
import com.xhwl.centiotdata.handler.account.AccountHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAccountCentService;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IProjectCentService;
import com.xhwl.centiotdata.suppliersystem.leopen.UserLeopenHandler;
import com.xhwl.centiotdata.utils.AccountUtil;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author pengjunming
 * @description
 * @date 2023/2/11 15:27
 */
@Service
@Slf4j
public class AccountLeopenHandler extends AccountHandler {

    @Resource
    private UserLeopenHandler userLeopenHandler;

    @Resource
    private IAccountCentService iAccountCentService;

    @Resource
    private IAuthAccountCentService iAuthPedestrianCentService;

    @Autowired
    private AccountUtil accountUtil;

    @Resource
    private IDeviceCentService iDeviceCentService;

    @Resource
    private IProjectCentService projectCentService;

    @Override
    public void add(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        handleAdd(dto, projectId, integrationConfig);
    }

    private void handleAdd(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        log.info("==同步账号数据到乐牛开始==");
        if (null != integrationConfig) {
            try {
                Integer productId = integrationConfig.getProductId();
                PedestrianIntegrationConfigDTO config = getConfig(integrationConfig, projectId);
                String thirdId = userLeopenHandler.addUser(config, dto);
                if (thirdId != null) {
                    iAuthPedestrianCentService.save(projectId, dto.getAccountId(), thirdId, null, productId);
                }
            } catch (Exception e) {
                log.error("账号信息：{},配置：{},异常：{}", dto, integrationConfig, e);
            }
        }
    }

    private PedestrianIntegrationConfigDTO getConfig(DeviceProjSupplierCategory integrationConfig, Integer projectId) {
        JSONObject properties = integrationConfig.getProperties();
        Integer productId = integrationConfig.getProductId();
        PedestrianIntegrationConfigDTO config = new PedestrianIntegrationConfigDTO();
        config.setIpAndPort(properties.getString("ipAndPort"));
        config.setAk(properties.getString("ak"));
        config.setSk(properties.getString("sk"));
        config.setProductId(productId);
        config.setPassword(properties.getString("password"));
        config.setProjectId(projectId);
        return config;
    }


    @Override
    public void update(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        if (null != integrationConfig) {
            //查询关联表
            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, dto.getAccountId(), integrationConfig.getProductId());
            //没查到关联关系，则新增
            if (null == authAccount) {
                handleAdd(dto, projectId, integrationConfig);
            } else {
                userLeopenHandler.updateUser(getConfig(integrationConfig, projectId), dto);
            }
        }
    }

    @Override
    public void delete(AccountDTO dto, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        if (null != integrationConfig) {
            //查询关联表
            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, dto.getId(), integrationConfig.getProductId());
            if (null != authAccount) {
                userLeopenHandler.deleteUser(getConfig(integrationConfig, projectId), dto);
                iAuthPedestrianCentService.deleteAccountMapping(projectId, dto.getId(), authAccount.getOpenId(), integrationConfig.getProductId());
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AccountFactory.register(ProductEnum.LEOPEN.getId(), this);
        log.info("乐牛账号管理注册成功");
    }
}
